﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;

namespace Segmentation.Business
{
    public static class BitmapCreater
    {
        #region Static Methods

        public static Bitmap GetBitmapFromJ(float[,] jData)
        {
            if (jData == null)
            {
                throw new ArgumentNullException("map");
            }
            int width = jData.GetLength(1);
            int height = jData.GetLength(0);

            Bitmap bitmap = new Bitmap(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    int grey = (int)(255 * jData[y, x] / 2);
                    Color color = Color.FromArgb(grey, grey, grey);
                    bitmap.SetPixel(x, y, color);
                }
            }
            return bitmap;
        }

        public static Bitmap GetBitmapFromJ(float[,] jData, Bitmap bitmap)
        {
            if (jData == null)
            {
                throw new ArgumentNullException("map");
            }
            if (bitmap == null)
            {
                throw new ArgumentNullException("bitmap");
            }
            int width = jData.GetLength(1);
            int height = jData.GetLength(0);

            Bitmap bitmapNew = new Bitmap(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    Color color = bitmap.GetPixel(x, y);
                    color = Color.FromArgb(
                        (int)(color.R * jData[y, x] / 2),
                        (int)(color.G * jData[y, x] / 2),
                        (int)(color.B * jData[y, x] / 2));
                    bitmap.SetPixel(x, y, color);
                }
            }
            return bitmap;
        }

        public static Bitmap GetBitmapFromMap(byte[,] map)
        {
            if (map == null)
            {
                throw new ArgumentNullException("map");
            }
            ColorCollection colors = new ColorCollection();
            Dictionary<byte, Color> colorClasses = new Dictionary<byte, Color>();

            int width = map.GetLength(1);
            int height = map.GetLength(0);

            Bitmap bitmap = new Bitmap(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    if (!colorClasses.ContainsKey(map[y, x]))
                    {
                        colorClasses.Add(map[y, x], colors.GetRandomColor());
                    }
                    bitmap.SetPixel(x, y, colorClasses[map[y, x]]);
                }
            }

            return bitmap;
        }

        public static Bitmap GetBitmapWithBounderies(byte[,] map, Bitmap bitmap, double displayIntensity)
        {
            if (map == null)
            {
                throw new ArgumentNullException("map");
            }
            if (bitmap == null)
            {
                throw new ArgumentNullException("bitmap");
            }
            int width = map.GetLength(1);
            int height = map.GetLength(0);

            Bitmap bitmapWithBounderies = new Bitmap(width, height);
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    Color color = bitmap.GetPixel(x, y);
                    color = Color.FromArgb(
                        (int)(color.R * displayIntensity),
                        (int)(color.G * displayIntensity),
                        (int)(color.B * displayIntensity));
                    bitmapWithBounderies.SetPixel(x, y, color);
                }
            }
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width - 1; x++)
                {
                    if (map[y, x] != map[y, x + 1])
                    {
                        bitmapWithBounderies.SetPixel(x, y, Color.White);
                        bitmapWithBounderies.SetPixel(x + 1, y, Color.White);
                    }
                }
            }
            for (int x = 0; x < width; x++)
            {
                for (int y = 0; y < height - 1; y++)
                {
                    if (map[y, x] != map[y + 1, x])
                    {
                        bitmapWithBounderies.SetPixel(x, y, Color.White);
                        bitmapWithBounderies.SetPixel(x, y + 1, Color.White);
                    }
                }
            }
            return bitmapWithBounderies;
        }

        #endregion
    }
}
